package com.example.logistics.controller;

import com.example.logistics.controller.dto.CreateOrderRequest;
import com.example.logistics.entity.CustomerOrder;
import com.example.logistics.entity.OrderStatus;
import com.example.logistics.repository.CustomerOrderRepository;
import com.example.logistics.service.OrderService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/orders")
@RequiredArgsConstructor
public class OrderController {
    private final OrderService orderService;
    private final CustomerOrderRepository orderRepository;

    @GetMapping
    public List<CustomerOrder> list() {
        return orderRepository.findAll();
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public CustomerOrder create(@Valid @RequestBody CreateOrderRequest request) {
        return orderService.createOrder(request);
    }

    @PatchMapping("/{id}/status")
    public CustomerOrder updateStatus(@PathVariable Long id, @RequestParam OrderStatus status) {
        return orderService.updateStatus(id, status);
    }
}


